// 右键菜单模块
const contextMenu = {
    menu: null,

    init() {
        this.menu = document.getElementById('contextMenu');
    },

    show(x, y) {
        const menuItems = this.menu.getElementsByTagName('div');
        
        if (tableSelection.selectedCells.length === 1) {
            const cell = tableSelection.selectedCells[0];
            const ismerged = (cell.rowSpan > 1 || cell.colSpan > 1);
            
            menuItems[0].style.display = 'none';
            menuItems[1].style.display = ismerged ? 'block' : 'none';
            menuItems[2].style.display = 'block';
            menuItems[3].style.display = 'block';
            menuItems[4].style.display = 'block';
            menuItems[5].style.display = 'block';
        } else if (tableSelection.selectedCells.length > 1) {
            menuItems[0].style.display = 'block';
            menuItems[1].style.display = 'none';
            menuItems[2].style.display = 'none';
            menuItems[3].style.display = 'none';
            menuItems[4].style.display = 'none';
            menuItems[5].style.display = 'none';
        } else {
            for (let i = 0; i < menuItems.length; i++) {
                menuItems[i].style.display = 'none';
            }
        }

        this.menu.style.display = 'block';
        
        const viewportWidth = window.innerWidth;
        const viewportHeight = window.innerHeight;
        const menuWidth = this.menu.offsetWidth;
        const menuHeight = this.menu.offsetHeight;

        if (x + menuWidth > viewportWidth) {
            x = viewportWidth - menuWidth;
        }
        if (y + menuHeight > viewportHeight) {
            y = viewportHeight - menuHeight;
        }

        this.menu.style.left = x + 'px';
        this.menu.style.top = y + 'px';
    },

    hide() {
        this.menu.style.display = 'none';
    }
}; 